import 'package:boss/ui/page/commission/page/commissionDetail/commission_detail_page.dart';
import 'package:boss/ui/page/commission/provider/commission_provider.dart';
import 'package:boss/ui/widget/loader/empty_widget.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'commission_item.dart';

class CommissionList extends StatefulWidget {
  final String status;

  const CommissionList(this.status, {Key key}) : super(key: key);

  @override
  _CommissionListState createState() => _CommissionListState();
}

class _CommissionListState extends State<CommissionList> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);

    return Consumer<CommissionProvider>(
      builder: (context, provider, child) {
        return LoaderPullRefresh(
          refreshTask: () => provider.queryCommissions(widget.status),
          builder: (context) {
            if (provider.data[widget.status].length == 0) {
              return EmptyWidget();
            }
            return ListView.builder(
              itemCount: provider.data[widget.status].length,
              itemBuilder: (context, index) {
                var current = provider.data[widget.status][index];

                return CommissionItem(
                  data: current,
                  onTap: () {
                    Navigator.pushNamed(context, CommissionDetailPage.pageName, arguments: {
                      'commissionId': current.id,
                      'commission': current,
                      'commissionStatus': widget.status,
                    });
                  },
                );
              },
            );
          },
        );
      },
    );
  }

  @override
  bool get wantKeepAlive => true;
}
